package me.tonywang.filter;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * @author wanghui
 * @since 2018/2/7 15:43
 */
public class FilterChain {

    List<Filter> additionalFilters = new ArrayList<>();

    private int currentPosition = 0;

    public void doFilter( ){
        System.out.println("FilterChain doFilter" + currentPosition );
        if (this.currentPosition == this.additionalFilters.size()) {
//            this.originalChain.doFilter(request, response);
        }
        else {
            this.currentPosition++;
            Filter  nextFilter= this.additionalFilters.get(this.currentPosition - 1);
            nextFilter.doFilter(  this);
        }
    }

    public void addFilter(Filter filter){
        additionalFilters.add(filter);
    }


    public static void main(String[] args) {
        FilterChain filterChain = new FilterChain();
        for (int i = 0; i <10 ; i++) {
            filterChain.addFilter( new Filter(i));
        }
        filterChain.doFilter();
    }

}
